// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: pengtianrui 2276608413@qq.com
 * @LastEditTime: 2024-12-23 16:46:58
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Material\MAT1Parser.cpp
 */
#include "Import/BDFImport/BulkData/Material/MAT1FParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Material/IMAT1FService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (MAT1F);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/MAT1F/MAT1F.md>
*/
// 以下需要修改
enum MAT1Fkey : std::uint8_t
{
    MAT1FKEY_MID = 2,
    MAT1FKEY_E   = 3,
    MAT1FKEY_G   = 4,
    MAT1FKEY_NU  = 5,
    MAT1FKEY_GE  = 9,
};

Common::Status BDF::BULKDATA::ParseMAT1F (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                          std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建一个用于保存MAT1材料数据的临时结构体对象
    FEM::MAT1FData tempMAT1F;

    // TODO(bug 001) 填写m_domainId的正确值
    // 目前m_domainId硬编码为1，需要进一步确认是否正确
    tempMAT1F.m_domainId = 1;

    // 记录哪些属性为空，默认所有属性都不为空

    // 初始化当前处理的字符串条目的ID
    int curIDKey = 1;

    // 遍历所有分割后的条目信息
    for (auto curStrKey : dividedEntryInfo)
    {
                // 判断当前条目是否为空（即是否全为空格）
        bool const isEmpty = is_all_space (curStrKey);

        // 如果当前条目为空，则记录对应的缺失项
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        // 根据条目ID，将解析后的数值存入对应的MAT1字段
        switch (curIDKey)
        {
        case MAT1FKEY_MID:
            tempMAT1F.m_id = convertToI64 (curStrKey);
            break;
        case MAT1FKEY_E:
            tempMAT1F.m_E = convertToI64 (curStrKey);
            break;
        case MAT1FKEY_G:
            tempMAT1F.m_G = convertToI64 (curStrKey);
            break;
        case MAT1FKEY_NU:
            tempMAT1F.m_NU = convertToI64 (curStrKey);
            break;
        case MAT1FKEY_GE:
            tempMAT1F.m_GE = convertToI64 (curStrKey);
            break;
        default:
            break;
        }
        curIDKey++;
    }

    // 将解析后的MAT1数据通过数据库服务工厂的IsotropicMaterial服务添加到数据库中
    return dbServiceFactory->get<IMAT1FService> ()->Add (tempMAT1F);
}